package cn.kidng.gaodedemo;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.amap.api.services.core.PoiItem;

import java.util.List;

/**
 * Created by KIDNG on 2016/4/15.
 */
public class SuggestionAdapter extends BaseAdapter {
    private Context mContext;
    private List<PoiItem> mList;
    private OnItemClickListener mItemClickListener;

    public SuggestionAdapter(Context context, List<PoiItem> list) {
        mContext = context;
        mList = list;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public PoiItem getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_suggestion, null, false);
            ViewHolder holder = new ViewHolder();
            holder.mTextView = (TextView) convertView.findViewById(R.id.item_suggestion);
            holder.mLocation = (TextView) convertView.findViewById(R.id.item_location);
            convertView.setTag(holder);
            final View finalConvertView = convertView;
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mItemClickListener != null) {
                        mItemClickListener.onItemClick(position, finalConvertView);
                    }
                }
            });
        }
        ViewHolder holder = (ViewHolder) convertView.getTag();
        holder.mTextView.setText(mList.get(position).getTitle());
        PoiItem item = mList.get(position);
        holder.mLocation.setText(item.getSnippet());
        return convertView;
    }

    public void setList(List<PoiItem> list) {
        mList = list;
        notifyDataSetChanged();
    }

    public void removeAll() {
        if (mList != null) mList.clear();
        notifyDataSetChanged();
    }

    public void setItemClickListener(OnItemClickListener listener) {
        mItemClickListener = listener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position, View view);
    }

    class ViewHolder {
        TextView mTextView;
        TextView mLocation;
    }
}
